package com.mkjb.controller.validator;

import com.mkjb.dto.CustomerFormDto;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

/**
 * Created by DingJie  on 2016/1/18.
 */
@Component
public class CustomerFormDtoValidator implements Validator {
    @Override
    public boolean supports(Class<?> aClass) {
        return CustomerFormDto.class.equals(aClass);
    }

    @Override
    public void validate(Object target, Errors errors) {
        CustomerFormDto formDto = (CustomerFormDto) target;
        validateTitleAndContent(formDto, errors);
    }

    private void validateTitleAndContent(CustomerFormDto formDto, Errors errors) {
        if (StringUtils.isEmpty(formDto.getName())) {
            errors.rejectValue("name", null, "客户姓名不能为空");
        }
        if (StringUtils.isEmpty(formDto.getPhone())) {
            errors.rejectValue("phone", null, "电话不能为空");
        }
        if (StringUtils.isEmpty(formDto.getAddress())) {
            errors.rejectValue("address", null, "地址不能为空");
        }
        if (StringUtils.isEmpty(formDto.getLogistics())) {
            errors.rejectValue("logistics", null, "物流公司不能为空");
        }
    }
}
